import Vue from 'vue'//Vue
import VueRouter from 'vue-router' //路由

import NotFound from '../components/404.vue'//404报错引入
import Home from '../views/Home.vue' //首页引入
import Manage from '../views/Manage.vue' //栏目管理引入
import Login from '../views/Login.vue'//登录引入
import Register from '../views/Register.vue'//注册引入

import User from '../views/profile/User.vue'//个人中心引入
import Compile from '../views/profile/Compile.vue'//编辑资料引入
import Follow from '../views/profile/Follow.vue'//我的关注引入
import Favorite from '../views/profile/Favorite.vue'//我的收藏引入
import Comment from '../views/profile/Comment.vue'//我的跟帖引入


import ArticleDetails from '../views/ArticleDetails.vue'//文章详情引入
import MoreComments from '../views/MoreComments.vue'//更多评论引入
import Search from '../views/Search.vue'//搜索更多引入



import Dome from '../views/Dome.vue'//测试引入

Vue.use(VueRouter)

const routes = [
  { path: '/', component: Home },//首页配置
  { path: '/manage', component: Manage },//栏目管理配置
  { path: '/login', component: Login },//登录页配置
  { path: '/register', component: Register },//注册页配置
  {//个人中心引入页配置
    path: '/user', component: User, meta: {
      jurisdiction: true
    }
  },
  {//编辑资料引入页配置
    path: '/compile', component: Compile, meta: {
      jurisdiction: true
    }
  },
  {//我的关注引入页配置
    path: '/follow', component: Follow, meta: {
      jurisdiction: true
    }
  },
  {//我的收藏引入页配置
    path: '/favorite', component: Favorite, meta: {
      jurisdiction: true
    }
  },
  {//我的跟帖引入页配置
    path: '/comment', component: Comment, meta: {
      jurisdiction: true
    }
  },
  {//文章详情引入页配置
    path: '/details/:id', component: ArticleDetails,
  },
  {//更多评论引入页配置
    path: '/moreComments/:id', component: MoreComments,
  },
  {//搜索引入页配置
    path: '/search', component: Search,
  },
  { path: '*', component: NotFound },//404报错引入
  { path: '/dome', component: Dome },//测试引入
]

const router = new VueRouter({//实例化router
  routes   //routes:routes 简写
})

// 添加全局路由守卫
// 拦截住所有跳转, 执行回调函数
router.beforeEach((to, from, next) => {
  // 如果我进入的地方是个人中心,编辑资料
  if (to.meta.jurisdiction) {
    // 判断 token 是否存在
    if (localStorage.getItem('token')) {
      // 存在则放行
      return next()
    } else {
      // 如果不存在,跳转到登录页
      return router.push('/login').catch(err => { })
    }
  }

  return next()
})
export default router
//    ||
/* new Vue{
    el: "",
    router
} */